package com.itheima.reggie.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.itheima.reggie.common.R;
import com.itheima.reggie.dto.SetmealDishDto;
import com.itheima.reggie.dto.SetmealDto;
import com.itheima.reggie.pojo.Dish;
import com.itheima.reggie.pojo.Setmeal;
import com.itheima.reggie.pojo.SetmealDish;
import com.itheima.reggie.service.DishService;
import com.itheima.reggie.service.SetMealDishService;
import com.itheima.reggie.service.SetMealService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.CacheEvict;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.stream.Collectors;

@RestController
@RequestMapping("/setmeal")
@Api(tags = "套餐相关接口")

public class SetMealController {
    @Autowired
    SetMealService setMealService;
    @Autowired
    SetMealDishService setMealDishService;
    @Autowired
    DishService dishService;
    /**
     * 套餐分页查询
     * @param page
     * @param pageSize
     * @param name
     * @return
     */
    @ApiOperation(value = "套餐分页查询接口")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "page",value = "页码",required = true),
            @ApiImplicitParam(name = "pageSize",value = "每页记录数",required = true),
            @ApiImplicitParam(name = "name",value = "套餐名称",required = false)
    })
    @GetMapping("/page")
    public R<Page<SetmealDto>> page(Integer page, Integer pageSize, String name) {
        Page<SetmealDto> pages = setMealService.selectPageByName(page, pageSize, name);
        return R.success(pages);
    }
    /**
     * 新增套餐
     * @param setmealDto
     * @return
     */
    @ApiOperation(value = "新增套餐接口")
    @PostMapping
    @CacheEvict(cacheNames = "setmeal",allEntries = true)
    public R save(@RequestBody SetmealDto setmealDto) {
        setMealService.saveWithDishes(setmealDto);
        return R.success("保存成功");
    }

    @GetMapping("/{id}")
    public R<SetmealDto> selectById(@PathVariable Long id) {
        SetmealDto setmealDto = setMealService.selectById(id);
        return R.success(setmealDto);
    }


    @PutMapping
    @CacheEvict(cacheNames = "setmeal",allEntries = true)
    public R update(@RequestBody SetmealDto setmealDto) {
        setMealService.updateSetmeal(setmealDto);
        return R.success("修改成功");
    }


    @PostMapping("/status/{status}")
    public R updateStatusByIds(@PathVariable Integer status, Long[] ids) {
        setMealService.updateStatusByIds(status, ids);
        return R.success("修改成功");
    }


    @DeleteMapping
    public R deleteByIds(Long[] ids) {
        setMealService.deleteWithDishes(ids);
        return R.success("删除成功");
    }

    @GetMapping("/list")
    @Cacheable(cacheNames = "setmeal",key = "#categoryId",unless = "#result.data.size()==0")
    public R<List<Setmeal>> list(Long categoryId, Integer status) {
        QueryWrapper<Setmeal> queryWrapper = new QueryWrapper<>();
        queryWrapper
                .eq("category_id", categoryId)
                .eq("status", status);
        List<Setmeal> list = setMealService.list(queryWrapper);
        return R.success(list);
    }

    @GetMapping("/dish/{setmealId}")
    public R<List<SetmealDishDto>> dish(@PathVariable Long setmealId) {
        QueryWrapper<SetmealDish> queryWrapper = new QueryWrapper<SetmealDish>().eq("setmeal_id", setmealId);
        List<SetmealDish> list = setMealDishService.list(queryWrapper);
        List<SetmealDishDto> collect = list.stream().map(setmealDish -> {
            SetmealDishDto setmealDishDto = new SetmealDishDto();
            BeanUtils.copyProperties(setmealDish, setmealDishDto);
            Dish dish = dishService.getById(setmealDish.getDishId());
            setmealDishDto.setImage(dish.getImage());
            return setmealDishDto;
        }).collect(Collectors.toList());


//        List<SetmealDish> dishList = setMealDishService.list(new QueryWrapper<SetmealDish>().eq("setmeal_id", setmealId));
//        List<Dish> collect = dishList.stream().map(setmealDish ->
//            dishService.getById(setmealDish.getDishId())
//        ).collect(Collectors.toList());
        return R.success(collect);
    }
}


